package _05处理流;

import java.io.*;

public class ObjectStreamDemo {

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

//		testWrite();


        testRead();

    }

    private static void testRead() throws IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("d:/stu.data"));

        Student stu = (Student) in.readObject();
        System.out.println(stu);

        Student stu2 = (Student) in.readObject();
        System.out.println(stu2);

        in.close();

    }

    private static void testWrite() throws FileNotFoundException, IOException {

        // 1 打开流
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d:/stu.data"));


        // 2. 写入对象
        Student stu = new Student();
        stu.id = 1;
        stu.name = "张三";
        stu.age = 18;
        out.writeObject(stu);

        Student stu2 = new Student();
        stu2.id = 2;
        stu2.name = "李四";
        stu2.age = 19;
        out.writeObject(stu2);

        // 3. 关闭文件
        out.close();

    }
}


//Exception in thread "main" java.io.NotSerializableException: _05处理流.Student
//at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1192)
//at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:352)
//at _05处理流.ObjectStreamDemo.testWrite(ObjectStreamDemo.java:37)
//at _05处理流.ObjectStreamDemo.main(ObjectStreamDemo.java:13)

// Student要支持序列化